package com.hxd.otc.module.wallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.my.bean.WalletContactsBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/8/6 0006.
 * description ${钱包联系人 Adapter}
 */
public class WalletContactAdapter extends BaseAdapter<WalletContactAdapter.WalletContactHolder, WalletContactsBean> {

    private final boolean mIsShowDelete;

    private OnDeleteContactListener onDeleteContactListener;

    public WalletContactAdapter(Context context, boolean isShowDelete) {
        super(context);
        mIsShowDelete = isShowDelete;
    }

    @Override
    protected void onBindHolder(@NonNull WalletContactHolder holder, final int position) {
        final WalletContactsBean item = getItem(position);
        holder.tvName.setText(item.tag);
        holder.tvAddress.setText(item.address);
        holder.tvCoinType.setText(item.coinSymbol);
        holder.tvDelete.setVisibility(mIsShowDelete ? View.VISIBLE : View.GONE);
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDeleteContactListener != null) {
                    onDeleteContactListener.onDeleteContact(position, item);
                }
            }
        });
        holder.clItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) mOnItemClickListener.onItemClick(position);
            }
        });
    }

    @NonNull
    @Override
    public WalletContactHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new WalletContactHolder(mInflater.inflate(R.layout.item_wallet_contact, parent, false));
    }

    static class WalletContactHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.tv_coin_type)
        TextView tvCoinType;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.cl_item)
        ConstraintLayout clItem;

        WalletContactHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnDeleteContactListener(OnDeleteContactListener onDeleteContactListener) {
        this.onDeleteContactListener = onDeleteContactListener;
    }

    public interface OnDeleteContactListener {
        void onDeleteContact(int position, WalletContactsBean walletContactsBean);
    }
}
